package com.ruoyi.network;


import com.ruoyi.ufox.domain.BisDevice;
import com.ruoyi.ufox.service.IBisDeviceService;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 在线设备管理器
 */
@Component
public class OnlineDeviceManager {

    @Autowired
    private IBisDeviceService deviceService;

    /**
     * 在线设备列表
     */
    private static Map<String, DeviceOnline> ONLINE_CHANNELS = new HashMap<>();

    /**
     * 将设备添加到在线设备列表
     */
    public void add(String deviceCode, Channel channel){

        if(!checkDeviceOnline(deviceCode)){

            //判断设备编号是否存在与数据库中
            BisDevice device = deviceService.getDeviceByDeviceCode(deviceCode);

            if(device==null){

                channel.close();
            }else{
                DeviceOnline deviceOnline = new DeviceOnline(deviceCode, new Date(), channel);
                deviceOnline.setDeviceId(device.getDeviceId());
                deviceOnline.setLastHeartBeat(new Date());

                ONLINE_CHANNELS.put(deviceCode, deviceOnline);

                //更新设备在线状态为1
                device.setOnline(1L);
                //deviceService.updateDevice(device);
                //修改数据库在线状态为0 updateBisDevice
               // deviceService.updateBisDevice(1,deviceCode);
            }

        }else{
            //更新心跳时间
            ONLINE_CHANNELS.get(deviceCode).setLastHeartBeat(new Date());
        }


    }

    public void monitorDeviceOnline(){
        ////如果设备超出心跳时间，没有发送心跳包，就认为设备已经离线，将设备从在线设备列表中移除

        for (String deviceCode : ONLINE_CHANNELS.keySet()){

            DeviceOnline deviceOnline = ONLINE_CHANNELS.get(deviceCode);

            //获取最后发送心跳的时间
            Date date1 = deviceOnline.getLastHeartBeat();

            Date now = new Date();

            long seconds = (now.getTime() - date1.getTime())/1000;

            //超过10秒钟没有发送心跳
            if(seconds>13){

                System.out.println("设备"+deviceCode+"超过"+seconds+"秒没有发送心跳，自动踢设备下线");

                deviceOnline.getChannel().close();
                ONLINE_CHANNELS.remove(deviceCode);
            }
        }


    }

    /**
     * 监测设备是否在线
     * @param deviceCode
     * @return
     */
    public boolean checkDeviceOnline(String deviceCode){

        if(ONLINE_CHANNELS.containsKey(deviceCode)){
            return true;
        }
        return false;
    }

    public Map<String, DeviceOnline> getAllOnlineDevices(){

        return ONLINE_CHANNELS;
    }

    public DeviceOnline getDeviceByCode(String deviceCode){

        return ONLINE_CHANNELS.get(deviceCode);
    }

    /**
     * 从在线设备列表中移除设备
     * @param channel
     */
    public void remove(Channel channel) {

        String deviceCode = null;

        for (String code: ONLINE_CHANNELS.keySet()) {
            DeviceOnline deviceOnline = ONLINE_CHANNELS.get(code);
            if(deviceOnline.getChannel().equals(channel)){
                deviceCode = code;
                break;
            }
        }

        if(deviceCode!=null){

            ONLINE_CHANNELS.remove(deviceCode);

            channel.close();

            //修改数据库在线状态为0
           // deviceService.updateBisDevice(false, deviceCode);
        }

    }

    /**
     * 根据设备编号获取Channel
     * @param deviceCode
     * @return
     */
    public Channel getChannelByDeviceCode(String deviceCode){

        if(ONLINE_CHANNELS.containsKey(deviceCode)){
            return ONLINE_CHANNELS.get(deviceCode).getChannel();
        }
        return null;
    }
}
